<?php
namespace Org\PHPExcel;
include "PHPExcel.php";

class Excel
{
    public function getExcel($filePath)
    {
        set_time_limit(0);
        ini_set("memory_limit", "1024M");
        $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
        \PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

        $objPHPExcel = \PHPExcel_IOFactory::load($filePath);
        $sheet_read_arr = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "M", "N", "O", "R", "S", "T", "U", "V");
        $currentSheet = $objPHPExcel->getSheet(0);// 通过页名称取得当前页
        $row_num = $currentSheet->getHighestRow();

        for ($i = 2; $i <= $row_num; $i++) {
            $cell_values = array();
            foreach ($sheet_read_arr as $cell_val) {
                $address = $cell_val . $i;// 单元格坐标
                // 读取单元格内容
                $cell_values[$cell_val]= $currentSheet->getCell($address)->getFormattedValue();
            }
          $ret[] = $this->addData($cell_values);
        }

        P($ret);
    }

    public function addData($value)
    {

        $data = array(
            'goodsid' => $value['A'],//商品id
            'goodsname' =>$value['B'] , //商品名称
            'goodsimg'  =>$value['C'], //商品主图
            'goodsshow'  => $value['D'], //商品详情页链接地址
            'goodsclass'  => $value['E'],//商品一级类目
            'goodslink'  => $value['F'],//淘宝客链接
            'goodsprice'  => $value['G'],//商品价格(单位：元)
            'goodsmoonsum' => $value['H'],//商品月销量
            'goodsratio'  => $value['I'],//收入比率(%)
            'goodscommission'  => $value['J'],//佣金
            'goodsshopname'  => $value['M'],//店铺名称
            'goodsshopclass'  => $value['N'],//平台类型
            'goodsticketid'  => $value['O'],//优惠券id
            'goodsticket'  => $value['R'],//优惠券面额
            'goodsstar'  => $value['S'],//优惠券开始时间
            'goodsend'  => $value['T'],//优惠券结束时间
            'goodsticketurl'  => $value['U'],//优惠券链接
            'goodsticketshare'  => $value['V']//商品优惠券推广链接
        );

        $today =strtotime(date('Y-m-d'));
        $model = M('goods');
        $where['goodsid'] = $data['goodsid'];
        $rs = $model->where($where)->find();
        if(!$rs){
            if(strtotime($data['goodsend']) >= $today ){
                $ret = $model->add($data);
                return $ret;
            }
        }

    }

}